package com.qf.controller;

import com.qf.entity.Book;
import com.qf.entity.User;
import com.qf.service.BookService;
import com.qf.service.UserService;
import com.qf.service.impl.BookServiceImpl;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
杨威
2021/9/13
22:16
*/
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
    UserService userService=new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        String checkNode = request.getParameter("checkNode");
        String code = (String)request.getSession().getAttribute("code");
        //输入的验证码是空的或者与服务器的验证码不一样就重定向到登录页面
        if(checkNode==null||!(checkNode.equalsIgnoreCase(code))){
            response.sendRedirect("/bookshop2/login.jsp");
            return;
        }

        User user = userService.login(userName, passWord);
        request.getSession().setAttribute("user",user);
        //如果user不等于null 查询所有数据，转发到展示所有书的controller
        if(user!=null){
            response.sendRedirect(request.getContextPath()+"/ShowAllBookController");
        }else {//重定向到登录页面
            response.sendRedirect("/bookshop2/login.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
